package com.valuegroup.smarttodo.ui.programme.goals.detail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.ui.todo.task.detail.TaskDetailTouchHelper;
import com.valuegroup.smarttodo.plugins.recyclerview.ItemTouchHelperAdapter;
import com.valuegroup.smarttodo.plugins.recyclerview.ItemTouchHelperViewHolder;
import com.valuegroup.smarttodo.util.Utils;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import static com.valuegroup.smarttodo.ui.programme.goals.detail.GoalsDetailAdapter.TYPE_CONTENT;

/**
 * 任务详情步骤拖拽及滑动效果
 */
public class GoalsDetailTouchHelper extends ItemTouchHelper.Callback {
    private static final String TAG = TaskDetailTouchHelper.class.getSimpleName();
    private final ItemTouchHelperAdapter mAdapter;
    private Context context;
    private static final boolean ICON_FOLLOW = true;
    private static final boolean DRAW_CIRCLE = false;
    private Paint paint;
    private Paint paint2;

    public GoalsDetailTouchHelper(ItemTouchHelperAdapter adapter, Context context) {
        mAdapter = adapter;
        this.context = context;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        System.out.println("TaskDetailTouchHelper.getMovementFlags");
        // 上下为拖动（drag），左右为滑动（swipe）
        // Enable drag up and down and right swipe in right direction
        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //final int dragFlags = ItemTouchHelper.ACTION_STATE_IDLE;
        final int swipeFlags = ItemTouchHelper.END | ItemTouchHelper.START;
        // final int swipeFlags =  ItemTouchHelper.END | ItemTouchHelper.STAR; Enable swipe in both direction
        return makeMovementFlags(dragFlags, swipeFlags);

    }

    @Override
    public long getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
        // return animationType == ItemTouchHelper.ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION : DEFAULT_SWIPE_ANIMATION_DURATION;
        return animationType == ItemTouchHelper.ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION : 350;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
        if (source.getItemViewType() != target.getItemViewType()) {
            return false;
        }

        // Notify the adapter of the move
        mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (viewHolder.getItemViewType() != TYPE_CONTENT) {
            return;
        }

        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public void onSwiped(final RecyclerView.ViewHolder viewHolder, int i) {
        // Notify the adapter of the dismissal
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition(), isLeftSlide);
    }


    @Override
    public int getBoundingBoxMargin() {
        return super.getBoundingBoxMargin();
    }


    private boolean isLeftSlide = false;

    @Override
    public void onChildDraw(final Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (viewHolder.getItemViewType() != TYPE_CONTENT) {
            return;
        }

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        // Fade out the view as it is swiped out of the parent's bounds
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            View itemView = viewHolder.itemView;
            final int icInterval = Utils.dpToPx(25);
            final int icDefault = Utils.dpToPx(0);
            paint = new Paint();


            Bitmap icon;
            if (dX > 0) {
                //右滑动
                isLeftSlide = false;
                icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_event_white);
                float iconIntervalTop = (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2;
                // Set color for right swipe
                paint.setColor(Color.BLUE);

                // Draw Rect with varying right side, equal to displacement dX
                c.drawRect((float) itemView.getLeft() + icDefault, (float) itemView.getTop(), dX + icDefault,
                        (float) itemView.getBottom(), paint);

                // 设置图标
                if (ICON_FOLLOW) {
                    // 跟随移动
                    c.drawBitmap(icon, dX - icInterval - icon.getWidth(), iconIntervalTop, paint);
                } else {
                    // 固定
                    c.drawBitmap(icon, (float) itemView.getLeft() + icInterval, iconIntervalTop, paint);
                }

                icon.recycle();

            } else {
                //左滑动
                isLeftSlide = true;
                icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_close_white);
                float iconIntervalTop = (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2;

                // Set color for right swipe
                //paint.setColor(ContextCompat.getColor(context, R.color.gray));
                paint.setColor(Color.RED);

                // Draw Rect with varying right side, equal to displacement dX
                c.drawRect((float) itemView.getRight() + icDefault, (float) itemView.getTop(), dX + icDefault,
                        (float) itemView.getBottom(), paint);

                // 画圆(定位及效果实现，但是圆超出了item的宽度)
                if (DRAW_CIRCLE) {
                    paint2 = new Paint();
                    paint2.setColor(Color.RED);
                    if (Math.abs(dX) > itemView.getWidth() / 3)
                        c.drawCircle((float) itemView.getWidth() - (float) Math.abs(dX) + icInterval + (icon.getWidth() / 2),
                                iconIntervalTop + (icon.getWidth() / 2),
                                (Math.abs(dX) - itemView.getWidth() / 3) * 2, paint2);
                }

                // 设置图标
                if (ICON_FOLLOW) {
                    // 跟随移动
                    c.drawBitmap(icon, (float) itemView.getWidth() - (float) Math.abs(dX) + icInterval, iconIntervalTop, paint);
                } else {
                    // 固定
                    c.drawBitmap(icon, (float) itemView.getRight() - icInterval - icon.getWidth(), iconIntervalTop, paint);
                }

                icon.recycle();
            }
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {

            // 加了头尾布局,需要判断view的类型
            if (viewHolder.getItemViewType() != TYPE_CONTENT) {
                return;
            }

            // Let the view holder know that this item is being moved or dragged
            ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemSelected(context);
        }

        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder.getItemViewType() != TYPE_CONTENT) {
            return;
        }

        super.clearView(recyclerView, viewHolder);

        // Tell the view holder it's time to restore the idle state
        ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemViewHolder.onItemClear(context);

    }

    @Override
    public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) {
        //  return super.getMoveThreshold(viewHolder);
        return 0.5f;
        //  return super.getMoveThreshold(0.5f);
    }

    @Override
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        //if (viewHolder instanceof RecyclerView.ViewHolder) return 1f;
        //return super.getSwipeThreshold(viewHolder);
        return 0.9f;
    }
}
